package jmine.tec.script.persistent.api;

import jmine.tec.script.persistent.dao.ScriptLanguageDAO;
import jmine.tec.script.persistent.dao.ScriptLanguageIdentifier;
import jmine.tec.script.persistent.domain.Script;
import jmine.tec.script.persistent.domain.ScriptLanguage;
import jmine.tec.script.persistent.domain.ScriptPurpose;
import bancosys.tec.persist.controller.PersistenceEnabledController;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Testador de scripts de INIT
 * 
 * @author lundberg
 */
public class InitScriptTester {

    private PersistenceEnabledController controller;

    /**
     * @param controller
     */
    public InitScriptTester(PersistenceEnabledController controller) {
        this.controller = controller;
    }

    public <C> C createContext(Class<C> ctxInterface, String purpose, ScriptLanguageIdentifier language, String body)
            throws BeanNotFoundException {
        BaseDAO<Script> dao = this.controller.getDAOFactory().getGenericDAO(Script.class);
        BaseDAO<ScriptPurpose> purposeDAO = this.controller.getDAOFactory().getGenericDAO(ScriptPurpose.class);
        ScriptLanguageDAO languageDAO = this.controller.getDAOFactory().getDAOByClass(ScriptLanguageDAO.class);
        ScriptLanguage lang = languageDAO.findBySystemIdentifier(language);
        ScriptPurpose purp = purposeDAO.findByNaturalKey(purpose);
        Script script = dao.createBean();
        script.setBody(body);
        script.setLanguage(lang);
        script.setPurpose(purp);
        return script.createContext(ctxInterface);
    }
}
